export function formatDuring(mss) {
    var days = parseInt(mss / (1000 * 60 * 60 * 24));
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = parseInt(mss % (1000 * 60) / 1000);
    return {
        days,
        hours,
        minutes,
        seconds
    };
}

/**
 * 格式化时间
 * @param time {Number} 毫秒
 * @param fmt {String} fom
 * @returns {*}
 */
export function formatTime(time, fmt) {
    let date = new Date(time);
    var obj = {
        "yy": date.getFullYear(),
        "MM": date.getMonth() + 1,                 //月份
        "dd": date.getDate(),                    //日
        "hh": date.getHours(),                   //小时
        "mm": date.getMinutes(),                 //分
        "ss": date.getSeconds(),                 //秒
        "qq": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    for (let key in obj) {
        fmt = fmt.replace(key, obj[key]);
    }
    return fmt;
}

/**
 * 解析解析歌词
 * @param millisecond {Number} 毫秒
 * @param lyrics {String} 歌词数据，例如 "[00:00.100]歌词1[00:01:120]歌词2"
 * @returns {null|{lyric: null, allLyric: *[], index: number, timeArr: *[]}}
 */
export function analysisLyric(millisecond, lyrics) {
    if (!millisecond || !lyrics) return null;
    let splitArr = lyrics.split("[");
    let allLyric = [];
    let timeArr = [];
    let index = -1;
    let lyric = null;
    for (let i = splitArr.length - 1; i > 0; i--) {
        let currentArr = splitArr[i].split("]");
        let currentTime = toMillisecond(currentArr[0]);
        allLyric[i] = currentArr[1];
        timeArr[i] = currentTime;
        if (millisecond > currentTime && index === -1) {
            index = i;
            lyric = currentArr[1];
        }
    }

    function toMillisecond(format) {
        let minutesAndSeconds = format.split(".");
        let millisecond = minutesAndSeconds[1];
        minutesAndSeconds = minutesAndSeconds[0].split(":");
        if (millisecond) {
            millisecond = millisecond.length < 3 ? parseInt(millisecond + "0") : parseInt(millisecond);
        } else {
            millisecond = 0;
        }
        let second = parseInt(minutesAndSeconds[1]);
        let minute = parseInt(minutesAndSeconds[0]);
        // 化毫秒
        // 分
        minute = (minute * 60 * 1000);
        second = (second * 1000)
        return minute + second + millisecond;
    }

    return {
        allLyric,
        index,
        lyric,
        timeArr
    }
}

/**
 * 数字转换为小单位计数
 * @param n
 * @returns {string}
 */
export function toShortUnit(n) {
    // if(n < 1000) return n;
    // else if(n > 1000 && n < 10000) return (n/1000).toFixed(1)+"k";
    if(n < 10000) return n;
    else {
        return (n/10000).toFixed(1)+"w";
    }
}

export function random(min,max){
    let c = max - min;
    return min + Math.round(Math.random()*c);
}